package com.funny.stock.trade.controller;

import com.funny.stock.base.util.PropertiesCopyUtil;
import com.funny.stock.base.util.ResultUtil;
import com.funny.stock.base.vo.Result;
import com.funny.stock.data.vo.SimpleRealtimeStockData;
import com.funny.stock.trade.dao.pojo.ComposeStockItemDO;
import com.funny.stock.trade.service.ComposeStockItemService;
import com.funny.stock.trade.vo.ComposeStockItemVO;
import com.funny.stock.user.util.UserSecurityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author pengl
 * @date 2020/05/17
 **/
@RestController
@RequestMapping("/compose/stock/item")
public class ComposeStockItemController {
    public static final Logger logger = LoggerFactory.getLogger(ComposeStockItemController.class);

    @Autowired
    ComposeStockItemService stockItemService;

    @GetMapping("/list")
    public Result<List<ComposeStockItemVO>> list(@RequestParam Long composeId){
        Long userId = UserSecurityUtil.getUserId();
        List<ComposeStockItemDO> stockItemDOS = stockItemService.queryStockItemList(userId,composeId);
        List<ComposeStockItemVO> composeStockItemVOS = PropertiesCopyUtil.copyPropertiesByList(stockItemDOS, ComposeStockItemVO.class);

        if(CollectionUtils.isEmpty(composeStockItemVOS))
            return ResultUtil.success(null);


        List<String> codeList = new ArrayList<>();
        for (ComposeStockItemVO item : composeStockItemVOS) {
            codeList.add(item.getStockCode());
        }
        List<SimpleRealtimeStockData> cacheDataList = stockItemService.queryCacheStockDataList(codeList);

        if(CollectionUtils.isEmpty(cacheDataList))
            return ResultUtil.success(composeStockItemVOS);

        Map<String, SimpleRealtimeStockData> dataMap = cacheDataList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(SimpleRealtimeStockData::getSymbol, Function.identity(), (k1, k2) -> k1));
        for (ComposeStockItemVO item : composeStockItemVOS) {
            SimpleRealtimeStockData stockData = dataMap.get(item.getStockCode());
            if(stockData == null) continue;

            item.setCurPrice(BigDecimal.valueOf(stockData.getPrice()));
            BigDecimal capital = BigDecimal.valueOf(stockData.getPrice()).multiply(BigDecimal.valueOf(item.getStockNumber()));
            BigDecimal profit = BigDecimal.valueOf(stockData.getPrice()).subtract(item.getCostPrice()).multiply(BigDecimal.valueOf(item.getStockNumber()));

            BigDecimal divide = profit.multiply(BigDecimal.valueOf(100)).divide(capital, 2, BigDecimal.ROUND_HALF_UP);
            item.setCapital(capital);
            item.setProfitMoney(profit);
            item.setProfitPercent(divide);

            item.setUpdown(stockData.getUpdown());
//            BigDecimal percent = BigDecimal.valueOf(stockData.getPercent())
//                    .multiply(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
            item.setPercent(stockData.getPercent() * 100);
        }

        return ResultUtil.success(composeStockItemVOS);
    }



    
}
